package com.jike.shop.cloud.mq;

import com.jike.shop.cloud.constants.MQConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MQProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendPaymentOutOfTimeCheckMessage(Long paymentId) {
        String destination = MQConstants.TOPIC_PAYMENT + ":" + MQConstants.TAG_PAYMENT_OUT_OF_TIME_CHECK;

        PaymentMessage paymentMessage = new PaymentMessage();
        paymentMessage.setPaymentId(paymentId);
        paymentMessage.setType(PaymentMessage.TYPE_OUT_OF_TIME);
        Message<PaymentMessage> message = new GenericMessage<>(paymentMessage);

        rocketMQTemplate.syncSend(destination, message, 30_000, 18);//2小时
    }

    public void sendPaymentStatusChangedMessage(Long paymentId, Integer status) {
        String destination = MQConstants.TOPIC_PAYMENT_RESULT + ":" + MQConstants.TAG_PAYMENT_STATUS_CHANGED;

        PaymentMessage paymentMessage = new PaymentMessage();
        paymentMessage.setPaymentId(paymentId);
        paymentMessage.setStatus(status);
        paymentMessage.setType(PaymentMessage.TYPE_STATUS_CHANGED);
        Message<PaymentMessage> message = new GenericMessage<>(paymentMessage);

        rocketMQTemplate.send(destination, message);
    }
}
